﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Founder.PKURG.CRM;

namespace Founder.PKURG.Test
{
    class Program
    {

        public static Guid CreateProjectApproval(Guid projectId, long itemname, string blockName, string approvalPerson, string oa)
        {
            CRMInfoOP op = new CRMInfoOP("isIFD=false;UserName=administrator;Password=Founder@123;Domain=test;OrgServiceUri=http://172.25.20.82/EPM/XRMServices/2011/Organization.svc");
            return op.Add(new CRMInfo()
            {
                Name = "epm_projectapproval",
                Fields = new List<FieldInfo>()
                {
                      new FieldInfo()
                      {
                            Name="epm_project",
                            Value=projectId,
                            IsForeign=true,
                            ForeignName="epm_projectinfo"
                      },
                      new FieldInfo()
                      {
                            Name="epm_itemname",
                            Value=itemname,
                             IsOption=true
                      },
                      new FieldInfo()
                      {
                            Name="epm_block",
                            Value=blockName
                      },
                      new FieldInfo()
                      {
                            Name="epm_newapproveperson",
                            Value=approvalPerson
                      },
                      new FieldInfo()
                      {
                            Name="epm_oa",
                            Value=oa
                      },
                      new FieldInfo()
                      {
                            Name="epm_starttime",
                            Value=DateTime.Now
                      }
                }
            });
        }

        static void Main(string[] args)
        {


            CreateProjectApproval(new Guid("3ACB01CB-CC9E-E311-8288-00155D141823"), 919900000, "地块", "OA", "审批人");


            //Founder.PKURG.CRM.AdminCrmConnect connect = new CRM.AdminCrmConnect();
            //connect.ConnectionString = "isIFD=false;UserName=administrator;Password=Founder@123;Domain=test;OrgServiceUri=http://172.25.20.82/EPM/XRMServices/2011/Organization.svc";
            //Microsoft.Xrm.Sdk.IOrganizationService service = connect.OrgService;
            // CRMInfoOP op = new CRMInfoOP("isIFD=false;UserName=crmadmin;Password=Founder@2013;Domain=Founder;OrgServiceUri=http://172.25.20.89/EPM/XRMServices/2011/Organization.svc");




            //op.Prefix = "new";
            //op.CreateEntity(new EntityInfo()
            //{
            //    DisplayName = "第三实体",
            //    SchemaName = "thirdEntity"
            //});

            //op.Prefix = "new";
            //op.CreateEntityFields("thirdEntity", new EntityInfoField()
            //{
            //    DisplayName = "dd",
            //    SchemaName = "ww",
            //     FType= EntityInfoFieldType.FloatType
            //});



            //op.Add(new CRMInfo()
            //{
            //    Name = "epm_projectplanningindex",
            //    Fields = new List<FieldInfo>()
            //    {
            //        new FieldInfo()
            //        { 
            //            Name="epm_projectinfoid",
            //            Value=new Guid("E32B60AB-DD6E-E311-9137-00155D141823"),
            //             IsForeign=true,
            //              ForeignName="epm_project"
            //        },
            //        //new FieldInfo()
            //        //{ 
            //        //    Name="new_ww",
            //        //    Value=new Decimal(155)
            //        //},
            //        new FieldInfo()
            //        { 
            //            Name="epm_name",
            //            Value="-"
            //        }
            //    }
            //});
            //List<dynamic> list = new List<dynamic>();

            //NewMethod(list);


            //foreach (var item in list)
            //{
            //    DoCreate(op, item);
            //}


        }

        private static void NewMethod(List<dynamic> list)
        {
            /*          list.Add(new
            {
                sql = @"SELECT [epm_ProjectInfoId] as id,epm_name
FROM [epm_ProjectInfo]
where [epm_ProjectInfoId] not in
(
    SELECT [epm_project]
    FROM epm_projectdevindex
    where [epm_project] is not null
)",
                tName = "epm_projectdevindex",
                rFName = "epm_project",
                rTName = "epm_projectinfo"
            });
            list.Add(new
            {
                sql = @"SELECT [epm_ProjectInfoId] as id,epm_name
FROM [epm_ProjectInfo]
where [epm_ProjectInfoId] not in
(
    SELECT [epm_project]
    FROM epm_projectdevstatus
    where [epm_project] is not null
)",
                tName = "epm_projectdevstatus",
                rFName = "epm_project",
                rTName = "epm_projectinfo"
            });

            //---
            list.Add(new
            {
                sql = @"SELECT epm_projectblockid as id,epm_name
FROM epm_projectblock
where epm_projectblockid not in
(
    SELECT epm_block
    FROM epm_projectblockplanningindex
    where epm_block is not null
)",
                tName = "epm_projectblockplanningindex",
                rFName = "epm_block",
                rTName = "epm_projectblock"
            });
            list.Add(new
            {
                sql = @"SELECT epm_projectblockid as id,epm_name
FROM epm_projectblock
where epm_projectblockid not in
(
    SELECT epm_block
    FROM epm_projectblockdevindex
    where epm_block is not null
)",
                tName = "epm_projectblockdevindex",
                rFName = "epm_block",
                rTName = "epm_projectblock"
            });
            list.Add(new
            {
                sql = @"SELECT epm_projectblockid as id,epm_name
FROM epm_projectblock
where epm_projectblockid not in
(
    SELECT epm_bloack
    FROM epm_projectblockdevstatus
    where epm_bloack is not null
)",
                tName = "epm_projectblockdevstatus",
                rFName = "epm_bloack",
                rTName = "epm_projectblock"
            });*/

            //----
            list.Add(new
                      {
                          sql = @"SELECT epm_projectstageid as id,epm_name
FROM epm_projectstage
where epm_projectstageid not in
(
    SELECT epm_stage
    FROM epm_projectstageplanningindex
    where epm_stage is not null
)",
                          tName = "epm_projectstageplanningindex",
                          rFName = "epm_stage",
                          rTName = "epm_projectstage"
                      });
            list.Add(new
            {
                sql = @"SELECT epm_projectstageid as id,epm_name
FROM epm_projectstage
where epm_projectstageid not in
(
    SELECT epm_stage
    FROM epm_projectstagedevindex
    where epm_stage is not null
)",
                tName = "epm_projectstagedevindex",
                rFName = "epm_stage",
                rTName = "epm_projectstage"
            });
            list.Add(new
            {
                sql = @"SELECT epm_projectstageid as id,epm_name
FROM epm_projectstage
where epm_projectstageid not in
(
    SELECT epm_stage
    FROM epm_projectstagedevstatus
    where epm_stage is not null
)",
                tName = "epm_projectstagedevstatus",
                rFName = "epm_stage",
                rTName = "epm_projectstage"
            });
            //
            //list.Add(new
            //{
            //    sql = "",
            //    tName = "epm_epm_projectstagedetailplanningindex",
            //    rFName = "",
            //    rTName = "epm_projectstage"
            //});
            //list.Add(new
            //{
            //    sql = "",
            //    tName = "epm_projectstagetypedevindex",
            //    rFName = "",
            //    rTName = "epm_projectstage"
            //});
            //list.Add(new
            //{
            //    sql = "",
            //    tName = "epm_projectstagetypedevstatus",
            //    rFName = "",
            //    rTName = "epm_projectstage"
            //});
            //list.Add(new
            //{
            //    sql = "",
            //    tName = "epm_projectstagetypesale",
            //    rFName = "",
            //    rTName = "epm_projectstage"
            //});
        }

        private static void DoCreate(CRMInfoOP op, dynamic obj)
        {
            Founder.PKURG.Common.DAL.ReturnInfo info = GetList(obj.sql);
            foreach (var item in info.MainData)
            {
                op.Add(new CRMInfo()
                {
                    Name = obj.tName,//"epm_projectplanningindex",
                    Fields = new List<FieldInfo>()
                {
                    new FieldInfo()
                    { 
                        Name=obj.rFName,//"epm_project",
                        Value=new Guid(item.id),
                         IsForeign=true,
                          ForeignName=obj.rTName//"epm_projectinfo"
                    },
                    //new FieldInfo()
                    //{ 
                    //    Name="new_ww",
                    //    Value=new Decimal(155)
                    //},
                    new FieldInfo()
                    { 
                        Name="epm_name",
                        Value="-"
                    }
                }
                });
            }
        }

        private static Founder.PKURG.Common.DAL.ReturnInfo NewMethod()
        {
            Founder.PKURG.Common.DAL.ExecuteService service = new Common.DAL.ExecuteService("Data Source=172.25.20.88;Initial Catalog=PKURG_MSCRM;User ID=sa;Password=pass@word1");
            string sql = @"('SELECT [epm_ProjectCompanyName]
      ,[epm_ProjectInfoId]
      ,[epm_name]
      ,[epm_DevType]
      ,[epm_ProjectCompany]
      ,[epm_landUsage]
      ,[epm_typedescription]
  FROM [EPM_MSCRM].[dbo].[epm_ProjectInfo]
  where [epm_ProjectInfoId] not in
  (
  SELECT 
      [epm_project]
  FROM [EPM_MSCRM].[dbo].[epm_projectplanningindex]
  )')";
            Founder.PKURG.Common.DAL.ReturnInfo info = service.GetListRequest(sql, null);
            return info;
        }
        private static Founder.PKURG.Common.DAL.ReturnInfo GetList(string sql)
        {
            Founder.PKURG.Common.DAL.ExecuteService service = new Common.DAL.ExecuteService("Data Source=172.25.20.88;Initial Catalog=EPM_MSCRM;User ID=sa;Password=pass@word1");
            sql = @"('" + sql + "')";
            Founder.PKURG.Common.DAL.ReturnInfo info = service.GetListRequest(sql, null);
            return info;
        }
    }



}
